// components/goods-detail/index.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    selectGoods: {
      type: Object,
      value: {},
      observer: function (newValue) {
        this.setData({
          price: newValue.spuSellPrice
        })
      }
    }
  },
  observers: {
    'selectGoods': function (newList) {
      let price = newList.spuSellPrice;
      newList.skuSpecList.forEach(spec => {
        if (spec.selectIndex != null) {
          const extraPrice = spec.specOptionList[spec.selectIndex].skuSellPrice
          price += extraPrice;
        }
      })
      this.setData({
        price: price
      })
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    selectGoods: {},
    price: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onSpecTap(e) {
      const {
        specOption,
        specIndex
      } = e.target.dataset;
      let selectGoods = this.data.selectGoods
      console.log("selectGoods", selectGoods);
      const index = selectGoods.skuSpecList[specIndex].specOptionList.findIndex(item => specOption.skuId == item.skuId)
      console.log("specIndex", specIndex);
      console.log("index", index);
      if (selectGoods.skuSpecList[specIndex].selectIndex != null && selectGoods.skuSpecList[specIndex].selectIndex === index) {
        selectGoods.skuSpecList[specIndex].selectIndex = null
        console.log("反选")
      } else {
        selectGoods.skuSpecList[specIndex].selectIndex = index
        console.log("正选")
      }
      this.setData({
        selectGoods
      })
      console.log("onSpecTap over");
    },
    addToCart() {
      const goods = this.data.selectGoods
      let propsText = ""
      let props = []
      let returnFlag = false;
      let mutlPrice = goods.spuSellPrice;
      goods.skuSpecList.forEach(skuSpec => {
        if (skuSpec.selectFlag) {
          if (skuSpec.selectIndex !== null) {
            const specOption = skuSpec.specOptionList[skuSpec.selectIndex]
            props.push(specOption.skuId)
            const specOptionValue = specOption.specOptionValue
            propsText += specOptionValue + "/"
            mutlPrice += specOption.skuSellPrice
          } else {
            app.showMsg("请选择" + skuSpec.specName)
            returnFlag = true
            return
          }
        } else {
          if (skuSpec.selectIndex !== null) {
            const specOption = skuSpec.specOptionList[skuSpec.selectIndex]
            props.push(specOption.skuId)
            const specOptionValue = specOption.specOptionValue
            propsText += specOptionValue + "/"
            mutlPrice += specOption.skuSellPrice
          }
        }
      })
      if (returnFlag) {
        return
      }
      console.log("propsText" + propsText)
      console.log("props" + props)
      if (propsText !== "") {
        propsText = propsText.substring(0, propsText.length - 1)
      }
      console.log("propsText" + propsText)
      let data = {
        goods: goods,
        propsText: propsText,
        props: props,
        mutlPrice: mutlPrice,
        number: "1"
      }
      this.triggerEvent('addToCartEvent', {
        data: data
      }, {
        bubbles: true,
        composed: true
      });
    }
  }
})